package safeCommitter.test.report;

import java.util.LinkedList;
import java.util.List;

import safeCommitter.model.TestType;
import safeCommitter.test.executor.common.TestResultType;

public class SCClazzTestResult implements ISCClassMethodTestResult {

	private String clazzName;

	List<SCTestResult> children = new LinkedList<SCTestResult>();

	private final String fullPath;

	public SCClazzTestResult(final String clazzName, final String fullPath) {
		this.clazzName = clazzName;
		this.fullPath = fullPath;
	}

	public void setClazzName(final String clazzName) {
		this.clazzName = clazzName;
	}

	public List<SCTestResult> getChildren() {
		return children;
	}

	public void setChildren(final List<SCTestResult> children) {
		this.children = children;
	}

	public void addResult(final SCTestResult result) {
		getChildren().add(result);

	}

	public TestResultType getStatus() {
		for (ISCTestResult test : getChildren()) {
			if (!test.getStatus().equals(TestResultType.SUCCESS)) {
				return TestResultType.FAILING;
			}
		}
		return TestResultType.SUCCESS;
	}

	public String getPath() {
		return fullPath;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		SCClazzTestResult clonedClazz = new SCClazzTestResult(new String(
				clazzName), new String(fullPath));
		List<SCTestResult> clonedChildren = new LinkedList<SCTestResult>();
		for (SCTestResult result : children) {
			clonedChildren.add((SCTestResult) result.clone());
		}
		clonedClazz.setChildren(clonedChildren);
		return clonedClazz;
	}

	public String getTimeSpent() {
		Long timeSpent = 0L;
		for (SCTestResult test : children) {
			timeSpent += Long.valueOf(test.getTimeSpent());
		}
		return timeSpent.toString();
	}

	public TestType getTestType() {
		return getChildren().get(0).getTestType();
	}

	public String getClassName() {
		return clazzName;
	}

	public String getDescription() {
		StringBuilder sb = new StringBuilder();
		for (SCTestResult methodTest : children) {
			sb.append(methodTest.getDescription()).append(",");
		}
		return sb.substring(0, sb.length() - 1);
	}

}
